package dk.pun.aot.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import dk.pun.aot.client.view.SelectCharacterView.Presenter;
import dk.pun.aot.shared.character.CharacterId;

public class CharacterRenameDialog extends DialogBox {

	private static CharacterRenameDialogUiBinder uiBinder = GWT.create(CharacterRenameDialogUiBinder.class);

	interface CharacterRenameDialogUiBinder extends UiBinder<Widget, CharacterRenameDialog> {}

	@UiField TextBox currentName;
	@UiField TextBox newName;
	@UiField Button okButton;
	
	private Presenter presenter;
	private CharacterId characterId;

	public CharacterRenameDialog(Presenter presenter, CharacterId characterId) {
		setWidget(uiBinder.createAndBindUi(this));
		setText("Rename Character");
		setAutoHideEnabled(true);
		setGlassEnabled(true);
		
		this.currentName.setText(characterId.getName());
		this.currentName.setEnabled(false);
		this.newName.setText(characterId.getName());
		this.newName.setFocus(true);
		
		this.presenter = presenter;
		this.characterId = characterId;
	}

	@UiHandler("okButton")
	public void okButtonClicked(ClickEvent event) {
		if (!this.newName.getText().isEmpty()) {
			this.characterId.setName(this.newName.getText());
			this.presenter.onCharacterRenamed(this.characterId);
		}
		this.hide();
	}
	
	@UiHandler("cancelButton")
	public void cancelButtonClicked(ClickEvent event) {
		this.hide();
	}

}
